package zlx.springcloud.controller;



import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import zlx.commons.entities.CommonResult;
import zlx.commons.entities.Payment;
import zlx.springcloud.lb.LoadBalancer;


import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

@RestController
@Slf4j
public class OderController {

//    public static final String PAYMNET_URL = "http://localhost:8001";
    public static final String PAYMNET_URL = "http://ZLX.SPRINGCLOUD";
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private LoadBalancer loadBalancer;

    @Resource
    private DiscoveryClient discoveryClient;


    @GetMapping(value = "/comsumer /payment/create")
    public CommonResult<Payment> create(Payment payment){
        return restTemplate.postForObject(PAYMNET_URL+"/payment/create",payment, CommonResult.class);
    }
    @GetMapping(value = "/comsumer /payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMNET_URL+"/payment/get/"+id,CommonResult.class);
    }
    @GetMapping(value = "/consumer/payment/lb")
    public String getPaymnetLB(){
        List<ServiceInstance> instances = discoveryClient.getInstances("ZLX.SPRINGCLOUD");
        if(instances == null || instances.size()<=0){
            return null;
        }
        ServiceInstance serviceInstance = loadBalancer.instance(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri+"/payment/lb",String.class);
    }
    @GetMapping(value = "" +
            "")
    public String zipkin(){
        return restTemplate.getForObject(PAYMNET_URL+"/payment/zinkin",String.class);
    }
}
